<template>
  <div class="sliders" ref="sliders">
    <ul class="slider-group clearfix" ref="sliderGroup">
      <li v-for="slider in slidersArr" class="slider_item">
        <a :href="slider.linkUrl">
          <img :src="slider.picUrl" alt="">
        </a>
      </li>
    </ul>
    <ul class="dots">
      <li class="dot" v-for="(dot,index) in dots" :class="{'active': curPageIndex === index}"></li>
    </ul>
  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll';
  import {addClass} from 'common/js/dom';
  import {getRecommend} from 'api/recommend';
  export default {
    props: {
      loop: {
        type: Boolean,
        default: true
      },
      autoPlay: {
        type: Boolean,
        default: true
      },
      interval: {
        type: Number,
        default: 3000
      }
    },
    data () {
      return {
        slidersArr: [],
        dots: [],
        curPageIndex: 0
      };
    },
    mounted () {
      this._getRecommend();
      setTimeout(() => {
        this._setSliderWidth();
        this._initDots();
        this._initSlider();
        if (this.autoPlay) {
          this._play();
        }
      }, 500);
      window.addEventListener('resize', () => {
        if (!this.sliders) {
          return;
        }
        this._setSliderWidth(true);
        this.sliders.refresh();
      });
    },
    components: {},
    methods: {
      _getRecommend () {
        getRecommend().then((res) => {
          this.slidersArr = res.data.slider;
          setTimeout(() => {
            this.children = this.$refs.sliderGroup.children;
            for (let i = 0; i < this.children.length; i++) {
              let child = this.children[i];
              addClass(child, 'slider_item');
            }
          }, 20);
        });
      },
      _setSliderWidth (isResize) {
        this.children = this.$refs.sliderGroup.children;
        let width = 0;
        let slidersWidth = this.$refs.sliders.clientWidth;
        for (let i = 0; i < this.children.length; i++) {
          let child = this.children[i];
//          addClass(child, 'slider-item');
          child.style.width = slidersWidth + 'px';
          width += slidersWidth;
        }
        if (this.loop && !isResize) {
          width += 2 * slidersWidth;
        }
        this.$refs.sliderGroup.style.width = width + 'px';
      },
      _initDots () {
        this.dots = new Array(this.children.length);
      },
      _initSlider () {
        this.sliders = new BScroll(this.$refs.sliders, {
          scrollX: true,
          scrollY: false,
          click: true,
          snap: {'loop': this.loop},
          snapLoop: this.loop,
          momentum: false,
          sliderTreshold: 0.3,
          snapSpeed: 400
        });
        this.sliders.on('scrollEnd', () => {
          let pageIndex = this.sliders.getCurrentPage().pageX;
          if (this.loop) {
            pageIndex -= 1;
            this.curPageIndex = pageIndex;
          }

          if (this.autoPlay) {
            clearTimeout(this.timer);
            this._play();
          }
        });
      },
      _play () {
        let pageIndex = this.curPageIndex + 1;
        if (this.loop) {
          pageIndex += 1;
        }
        this.timer = setTimeout(() => {
          this.sliders.goToPage(pageIndex, 0, 400);
        }, this.interval);
      }
    }
  };
</script>

<style lang='scss'>
  .sliders {
    min-height: 1px;
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    .slider_item {
      float: left;
      overflow: hidden;
      a {
        display: block;
        width: 100%;
        height: 100%;
        img {
          width: 100%;
        }
      }

    }
    .dots {
      position: absolute;
      width: 100%;
      bottom: 10px;
      left: 0;
      text-align: center;
      .dot {
        display: inline-block;
        width: 10px;
        height: 10px;
        margin: 0 5px;
        border-radius: 5px;
        background: #ddd;
        &.active {
          width: 20px;
          background: #fff;
        }
      }
    }
  }

</style>
